package com.UnionPayCenter.service.serviceImpl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.UnionPayCenter.entity.ManagerEntity;
import com.UnionPayCenter.entity.vo.DataBoardHistoryDataVo;
import com.UnionPayCenter.entity.vo.DataBoardTodayDataVo;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.DataBoardMapper;
import com.UnionPayCenter.mapper.ManagerMapper;
import com.UnionPayCenter.service.IDataBoardService;
import com.UnionPayCenter.util.DateFormatUtil;
import com.alibaba.fastjson.JSONObject;

@Service
@Transactional
public class DataBoardServiceImpl implements IDataBoardService {
    @Autowired
    private ManagerMapper managerMapper;
    @Autowired
    private DataBoardMapper dataBoardMapper;

    @Override
    public DataBoardTodayDataVo getTodayData(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            Date startTime = DateFormatUtil.getTodayStartTime(new Date());
            Date endTime = DateFormatUtil.dayCalculate(startTime, 1);
            String type = "Y";
            
            Integer chargeAmount = dataBoardMapper.getTodayChargeAmount(shopId, startTime, endTime);
            Integer newUser = dataBoardMapper.getRegisterUserCount(shopId, startTime, endTime, type);
            Integer deliveredCoupon = dataBoardMapper.getDeliveredCouponCount(shopId, startTime, endTime, type);
            Integer paidOrder = dataBoardMapper.getPaidOrderCount(shopId, startTime, endTime, type);
            Integer paidAmount = dataBoardMapper.getPaidAmount(shopId, startTime, endTime, type);
            
            DataBoardTodayDataVo todayData = new DataBoardTodayDataVo();
            todayData.setChargeAmount(chargeAmount);
            todayData.setNewUser(newUser);
            todayData.setDeliveredCoupon(deliveredCoupon);
            todayData.setPaidOrder(paidOrder);
            todayData.setPaidAmount(paidAmount);
            
            return todayData;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public DataBoardHistoryDataVo getHistoryData(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            Date cutoffTime = DateFormatUtil.getTodayStartTime(new Date());
            
            Integer totalAmount = dataBoardMapper.getHistoryTotalAmount(shopId, cutoffTime);
            Integer totalUser = dataBoardMapper.getRegisterUserCount(shopId, cutoffTime, null, null);
            Integer totalDeliveredCoupon = dataBoardMapper.getDeliveredCouponCount(shopId, cutoffTime, null, null);
            Integer totalPaidOrder = dataBoardMapper.getPaidOrderCount(shopId, cutoffTime, null, null);
            Integer totalPaidAmount = dataBoardMapper.getPaidAmount(shopId, cutoffTime, null, null);
            Integer totalUsedCoupon = dataBoardMapper.getUsedCouponCount(shopId, cutoffTime);
            
            DataBoardHistoryDataVo historyData = new DataBoardHistoryDataVo();
            historyData.setTotalAmount(totalAmount);
            historyData.setTotalUser(totalUser);
            historyData.setTotalDeliveredCoupon(totalDeliveredCoupon);
            historyData.setTotalPaidOrder(totalPaidOrder);
            historyData.setTotalPaidAmount(totalPaidAmount);
            historyData.setTotalUsedCoupon(totalUsedCoupon);
            
            return historyData;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

}
